package com.bc.bcapi.service;

import cn.hutool.core.util.StrUtil;
import com.baidu.disconf.client.utils.StringUtil;
import com.bc.bcapi.dao.*;
import com.bc.bccore.webconfig.WebConfig;
import com.bc.bcmodel.domain.*;
import com.bc.bcmodel.req.*;
import com.bc.bcmodel.resp.ElitesAnalysisListBaseResp;
import com.bc.bcmodel.resp.ElitesAnalysisListResp;
import com.bc.bcmodel.resp.ElitesAnalysisResp;
import com.bc.bcmodel.resp.ElitsListResp;
import com.bc.commoncore.util.DateUtil;
import com.bc.commoncore.util.StringUtils;
import com.bc.commonentity.common.JsonResult;
import com.bc.commonentity.enums.HttpReturnRnums;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.math.BigInteger;
import java.util.stream.Collectors;

/**
 * @Name T_User_elitesService * @Description  * @Author generation * @Date 2018/8/12 20:44:12 * @Version 1.0
 **/
@Service
public class T_User_elitesService {
    @Resource
    T_User_elitesMapper t_User_elitesMapper;
    @Resource
    T_User_buyer_infoMapper t_user_buyer_infoMapper;
    @Resource
    T_User_elites_relationMapper t_user_elites_relationMapper;
    @Resource
    T_Project_analysisMapper t_project_analysisMapper;
    @Resource
    T_User_seller_bonusMapper t_user_seller_bonusMapper;
    @Resource
    T_Project_analysis_relationMapper t_project_analysis_relationMapper;
    @Resource
    T_File_upload_logMapper t_file_upload_logMapper;
    @Resource
    T_File_upload_logService t_file_upload_logService;

    /**
     * 保存单条数据     *      * @return
     */

    public int insert(T_User_elitesModel req) {
        return t_User_elitesMapper.insert(req);
    }

    /**
     * 批量增加数据     *      * @return
     */

    public int insertBatch(List<T_User_elitesModel> req) {
        return t_User_elitesMapper.insertBatch(req);
    }

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */

    public T_User_elitesModel findByNamedParam(T_User_elitesModel req) {
        return t_User_elitesMapper.findByNamedParam(req);
    }

    /**
     * 根据对象查询所有数据     *      * @return
     */

    public List<T_User_elitesModel> getAllList(T_User_elitesModel req) {
        return t_User_elitesMapper.findByNamedParamList(req);
    }

    /**
     * 根据对象查询所有分页数据     *      * @return
     */

    public PageInfo<T_User_elitesModel> getList(T_User_elitesModel req) {
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<T_User_elitesModel> list = t_User_elitesMapper.findByNamedParamList(req);
        return new PageInfo<T_User_elitesModel>(list);
    }

    /**
     * 更新指定对象     *      * @return
     */

    public int update(T_User_elitesModel req) {
        return t_User_elitesMapper.update(req);
    }

    /**
     * 删除指定数据     *      * @return
     */

    public int delete(BigInteger id) {
        return t_User_elitesMapper.delete(id);
    }

    /**
     * 根据条件统计数据量     *      * @return
     */

    public long count(T_User_elitesModel req) {
        return t_User_elitesMapper.count(req);
    }

    /**
     * 申请人才
     */
    public JsonResult applyElits(UserElitesApplyReq req) {
        JsonResult js = new JsonResult();
        //获取当前用户是否已是人才，或已经申请
        //所属类型:1项目分析 2代码分析
        if (!checkElitesCount(req) ||
                (!StringUtils.isEmpty(req.getIntroduction()) && req.getIntroduction().length() > 300)) {
            return new JsonResult(HttpReturnRnums.ParaError);
        }

        String resume = t_file_upload_logService.setFilePath(req.getResume());
        T_User_elitesModel t_user_elitesModel = new T_User_elitesModel();
        t_user_elitesModel.setUser_id(req.getUser_id());
        T_User_elitesModel resultModel = t_User_elitesMapper.findByNamedParam(t_user_elitesModel);
        int typeid = req.getType_id();
        t_user_elitesModel.setLinkedin(req.getLinkedin());
        t_user_elitesModel.setResume(resume);
        t_user_elitesModel.setIntroduction(req.getIntroduction());
        t_user_elitesModel.setNick_name(req.getNick_name());
        if (resultModel == null) {
            if (typeid == 1) {
                t_user_elitesModel.setIs_business_status(2);
            }
            if (typeid == 2) {
                t_user_elitesModel.setIs_code_status(2);
            }
            t_User_elitesMapper.insert(t_user_elitesModel);
            return js;
        }

        if (typeid == 1 && resultModel.getIs_business_status() == 0) {
            t_user_elitesModel.setIs_business_status(2);
        } else if (typeid == 2 && resultModel.getIs_code_status() == 0) {
            t_user_elitesModel.setIs_code_status(2);
        }
//        else {
//
//            return new JsonResult(HttpReturnRnums.USER_ELITS_EXIST);
//        }
        t_User_elitesMapper.update(t_user_elitesModel);
        return js;
    }

    /**
     * 判断人才数量
     */
    private Boolean checkElitesCount(UserElitesApplyReq req) {
        int count = t_User_elitesMapper.getElitsCount(req.getUser_id());
        if (count > 0) {
            return true;
        }
        if (StringUtils.isEmpty(req.getNick_name()) ||
                (StringUtils.isEmpty(req.getLinkedin()) &&
                        StringUtils.isEmpty(req.getResume())) ||
                StringUtils.isEmpty(req.getIntroduction())
                ) {
            return false;
        }
        return true;
    }

    /**
     * 查询人才状态,人才信息
     */
    public JsonResult getElitesInfo(ElitesInfoReq req) {
        JsonResult js = new JsonResult();
        T_User_elitesModel t_user_elitesModel = new T_User_elitesModel();
        t_user_elitesModel.setUser_id(req.getUser_id());
        T_User_elitesModel result =
                t_User_elitesMapper.findByNamedParam(t_user_elitesModel);
        if (result == null) {
            return new JsonResult(HttpReturnRnums.USER_ELITS_NOTEXIST);
        }
        /*计算第一次加入专家时间**/
        if (result.getIs_code_status() == 1 && result.getIs_business_status() == 1) {
            result.setFirst_join_time(result.getBusiness_auth_time());
            if (result.getBusiness_auth_time().compareTo(result.getCode_auth_time()) > 0) {
                result.setFirst_join_time(result.getCode_auth_time());
            }
        } else if (result.getIs_business_status() == 1) {
            result.setFirst_join_time(result.getBusiness_auth_time());
        } else if (result.getIs_code_status() == 1) {
            result.setFirst_join_time(result.getCode_auth_time());
        }
        /*取用户头象**/
        String user_id = result.getUser_id();
        T_User_buyer_infoModel t_user_buyer_infoModel = new T_User_buyer_infoModel();
        t_user_buyer_infoModel.setUser_id(result.getUser_id());
        T_User_buyer_infoModel resultUserModel = t_user_buyer_infoMapper.findByNamedParam(t_user_buyer_infoModel);
        String head_figure = resultUserModel == null ? "" : WebConfig.getImgUrl() + resultUserModel.getHead_figure();
        result.setHead_figure(head_figure);
        if (result.getUser_id().equals(req.getUser_id())) {
            result.setIs_show_update(1);
        }
        if (!StrUtil.isEmpty(result.getResume())) {
            T_File_upload_logModel t_file_upload_logModel = new T_File_upload_logModel();
            t_file_upload_logModel.setFile_src(result.getResume());
            T_File_upload_logModel resumeModel = t_file_upload_logMapper.findByNamedParam(t_file_upload_logModel);
            if (resumeModel != null) {
                resumeModel.setFile_src(WebConfig.getImgUrl() + resumeModel.getFile_src());
            }
            result.setResumeModel(resumeModel);
        }
        js.setData(result);
        return js;
    }

    /**
     * 获取人才列表
     */
    public JsonResult getElitesList(ElitesListReq req) {
        JsonResult js = new JsonResult();
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<ElitsListResp> list =
                t_User_elitesMapper.getElitsList(req);
        if (req.getIs_favorite() != null || req.getIs_favorite() == 1) {
            /**
             * 拼装我收藏
             * */
            if (!StringUtils.isEmpty(req.getUser_id())) {
                //查询所有用户
                List<String> listUser =
                        t_user_elites_relationMapper.getFavoriteUserId(req.getUser_id());
                for (ElitsListResp item :
                        list) {
                    if (listUser.contains(item.getUser_id())) {
                        item.setIs_favorite(1);//标记为是收藏用户
                    }
                }
            }
        }
        PageInfo<ElitsListResp> pageInfo = new PageInfo<ElitsListResp>(list);
        js.setData(pageInfo);
        return js;
    }

    /**
     * 我的专家-项目分析列表
     */
    public JsonResult getMyElitesAnalysisList(ElitesAnalysisListReq req) {
        JsonResult js = new JsonResult();
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<ElitesAnalysisListResp> resp = t_project_analysisMapper.getElitesProject(req);
        List<String> listPid = new ArrayList<String>();
        List<String> listIsBonusUserId = new ArrayList<String>();
        for (ElitesAnalysisListResp item : resp) {
            if (item.getIssetbonus() == 1) {
                listIsBonusUserId.add(item.getSeller_uesr_id());
            }
            listPid.add(item.getPid());
        }
        //查询bonus
        IdsReq idsBonus = new IdsReq();
        idsBonus.setList(listIsBonusUserId);
        List<T_User_seller_bonusModel> listBonus = null;
        if (listIsBonusUserId != null && listIsBonusUserId.size() > 0) {
            listBonus = t_user_seller_bonusMapper.getBonusByUserIds(idsBonus);
        }
        //查询分析
        IdsReq idsAna = new IdsReq();
        List<ElitesAnalysisListBaseResp> listAnalysis = null;
        if (listPid != null && listPid.size() > 0) {
            idsAna.setList(listPid);
            listAnalysis = t_project_analysisMapper.getElitesAnalysislist(idsAna);
        }
//查询顶踩
//        List<String> listAid = new ArrayList<String>();
//        for (ElitesAnalysisListBaseResp item :
//                listAnalysis) {
//            listAid.add(item.getAid());
//        }
//        HashMap<String, Object> hashAnaRelation = new HashMap<String, Object>();
//        hashAnaRelation.put("user_id", req.getCookie_uesr_id());
//        hashAnaRelation.put("listAid", listAid);
//        List<T_Project_analysis_relationModel> listAnaRelation =
//                t_project_analysis_relationMapper.getGoodAndDespise(hashAnaRelation);
        for (ElitesAnalysisListResp item : resp) {
            //设置bonus
            if (listBonus != null && listBonus.size() > 0) {
                List<T_User_seller_bonusModel> list = listBonus.stream().
                        filter(t -> t.getUser_id().equals(item.getSeller_uesr_id())).
                        collect(Collectors.toList());
                if (list != null && list.size() > 0) {
                    item.setListBonus(list);
                }
            }
            //设置分析
            if (listAnalysis != null && listAnalysis.size() > 0) {
                List<ElitesAnalysisListBaseResp> listAna = listAnalysis.stream().
                        filter(t -> t.getPid().equals(item.getPid())).
                        collect(Collectors.toList());
                if (listAna != null && listAna.size() > 0) {
                    item.setListAna(listAna);
                }
            }
            item.setImg_project_logo(WebConfig.getImgUrl() + item.getImg_project_logo());
        }
        PageInfo<ElitesAnalysisListResp> pageInfo =
                new PageInfo<ElitesAnalysisListResp>(resp);
        ElitesAnalysisResp p = new ElitesAnalysisResp();
        p.setListInfo(pageInfo);
        js.setData(p);
        return js;
    }

    public JsonResult getMyElitesAnalysisByPid(ElitesByPidReq req) {
        T_Project_analysisModel t_project_analysisModel = new T_Project_analysisModel();
        t_project_analysisModel.setPid(req.getPid());
        t_project_analysisModel.setUser_id(req.getUser_id());
        t_project_analysisModel.setType_id(req.getType_id());
        T_Project_analysisModel result = t_project_analysisMapper.findByNamedParam(t_project_analysisModel);
        return JsonResult.get(result);
    }
}